#!/bin/bash
##这是一个仅用于Centos7.X及Centos8.x发行版安装部署K8S集群的脚本
##By:坐公交也用券@今日头条
help() {
    echo "Usage:"
    echo "Shell.sh [-i 指令 ] [-p 验证码 ]"
    echo "描述:传入语法及作用
     [ -i  ]; 通过 [ -i ] 传入指令
	 	install #进入全新安装程序
		reset-docker #配置Docker镜像加速
     [ -p 验证码 ]; 通过 [ -p ]传入验证码
     By：坐公交也用券@今日头条"
    exit 1
}
while getopts 'i:p:h' OPT; do
	case $OPT in
        'i') cmd="$OPTARG"
		echo -e "通过[ -c ]传入了指令: [ $cmd ]";;
		'p') bash ./config/Verification_Code.VC "123";;
        'h') help;;
        '-help') help;;
    esac
done
mkdir -p /init/
local_hostname_set(){
	input "请输入本机主机名,默认[ ${1} ]" "names"
	if [[ -z "$names" ]];then
		names="$1"
		red_z_green_z_info "已使用默认主机名" "$names"
	else
		red_z_green_z_info "已使用自定义主机名:" "${names}"
	fi
	if [[ "${1}" == "master" ]];then
		Master_server="$names"
	fi
	hostnamectl set-hostname $names
}
hosts_set(){
	for((i=1;i<=$1;i++));do
		input "请输入第${i}个Node节点主机名" "n_n"
		input "请输入${n_n}节点IP地址[不做网络连通性检测]" "n_ip"
		echo -e "${n_ip} ${n_n}" >> /etc/hosts 
		echo -e "${n_ip} ${n_n}" >> info.conf
	done
}
#获取安装节点信息
config_installer(){
	cpus=`cat /proc/cpuinfo| grep "processor"| sort| uniq| wc -l`
	if [[ "$cpus" -lt "2" ]];then
		red_z_green_z_info "本机CPU核心数量无法满足Master最低要求[最低2核]\n" "已默认设置本节点为Node节点"
	else
		red_z_green_z "请选择本机节点类型\n" "[1]Master\n" "[2]Node [Default]"
		read types
	fi
	input "是否初始化主机名映射文件[y/n],默认[ y ]" "inits"
	if [[ "$inits" == 'n' ]];then
		green_zt "保留现有hosts文件"
	else
		echo -e "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1         localhost localhost.localdomain localhost6 localhost6.localdomain6" > /etc/hosts
	fi
	input "先拉取镜像再配置服务:[y/n];默认[n]" "pull"
	if [[ "$types" == '1' ]];then
		node_type='master'
		input "是否生成初始化K8S脚本[y/n];默认[n]" "ik8s"
		input "请输入Node节点数量" "Node_s"
		if [[ -z "$Node_s" ]];then
			green_zt "未设置Node节点"
		else
			if [[ "$Node_s" -ge "1" ]];then
				hosts_set "$Node_s"
			else
				red_z_green_z "数量输入错误" "本次默认未设置Node节点"
			fi
		fi
		local_hostname_set "master"
		master_ip=$local_ip
	else
		node_type='node'
		input "请输入Matser节点主机名:" "Master_server"
		local_hostname_set "Node"
	fi
}
get_node_info(){
	for((i=1;i<=$hs;i++));do
		ip=`cat info.conf | sed -n "$i"p | awk '{print $1}'`
		hostn=`cat info.conf | sed -n "$i"p | awk '{print $2}'`
		red_z_green_z_info "${hostn}主机IP" "${ip}"
	done
}
get_master_info(){
	red_z_green_z_info "当前系统版本:" "[ CentOS${s} ]"
	red_z_green_z_info "当前主机类型:" "[ $node_type ]"
	red_z_green_z_info "Master节点IP:" "[ $master_ip ]"
	red_z_green_z_info "Master主机名:" "[ $Master_server ]"
	echo "$master_ip $Master_server" >> /etc/hosts
	if [[ "$ik8s" == "y" || "$ik8s" == "Y" ]];then
		ik='是'
	else
		ik="否"
	fi
	red_z_green_z_info "是否生成初始化K8S服务脚本" "$ik"
	if [[ "$pull" == "y" || "$pull" == "Y" ]];then
		 pl="是"
	else
		pl="否"
	fi
	red_z_green_z_info "服务安装完成立即拉取镜像" "$pl"
	input "按回车开始安装" Y
	echo -e "
	OS=CentOS${s}
	Host_type=$node_type
	master_ip=$master_ip
	Master_server=$Master_server" > /init/installer.conf
}

service_ver(){	
	red_z_green_z_info "当前Docker客户端版本" "$Dcoker_C_ver"
	red_z_green_z_info "当前Docker服务端版本" "$Dcoker_S_ver"
	red_z_green_z_info "当前kubernetes 版 本" "$kubeamd_ver"
}


#########################################执行安装步骤###########################################

if [[ "$cmd" == "install" ]];then
	Entering "系统类型判断"
	Check_Os_Type "CentOS"

	Entering "系统版本判断"
	Centos_Version_Number "7" "8"

	Entering "用户判断"
	Check_User 'root'

	Entering "网络连接状态检测"
	Check_Network

	Entering "IP地址检测与配置"
	Check_IP


	Entering "本机硬件配置检测"
	config_installer


	Entering "预安装信息确认"
	get_master_info

	Entering "源备份"
	yum_bak

	Entering "系统源配置From CentOS7"
	yum_config_os_centos7

	Entering "Docker源配置"
	yum_docker

	Entering "K8S源配置"
	yum_k8s


	Entering "防火墙配置"
	disable_firealld

	Entering "路由转发设置"
	sysconfig

	Entering "Docker服务安装"
	install_docker

	Entering "K8S服务安装"
	install_k8s

	Entering "Docker镜像加速及驱动配置"
	docker_config

	if [[ "$pull" == "y" || "$pull" == "Y" ]];then
		Entering "Docker镜像拉取"
		images_pull
		chek_status "$?" "镜像拉取"
	else
		green_zt "跳过镜像下载,请在控制节点执行下面的命令获取注册命令"
	fi

	Entering "K8S配置"
	k8s_config
elif [[ "$cmd" -eq "reset-docker" ]];then
	docker_tools='./tools/reset_Docker_mirrors.sh'
	Loading_function "$docker_tools"
fi